package com.qbb.server.bean.read;

import java.lang.reflect.Method;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import com.qbb.server.constanct.QbbConfigUtil;
import com.qbb.server.constanct.bean.QbbConfigBean;
import com.qbb.server.exception.HandlerException;
import com.qbb.server.security.SecurityUtil;
import com.qbb.server.util.JsonUtil;

/**
 * 类说明
 * 
 * @author chenhuaiao
 * @version V1.0 创建时间：Mar 12, 2015 5:16:36 PM
 */
public class SocketBean {
	private static Log log = LogFactory.getLog(SocketBean.class);
	private String uuIdStr;
	private String model;
	private String method;

	public String getUuIdStr() {
		return uuIdStr;
	}

	public void setUuIdStr(String uuIdStr) {
		this.uuIdStr = uuIdStr;
	}

	public Object getParams() {
		Class clas = this.getClass();
		String simpleName = clas.getSimpleName();
		try {
			if ("SocketListMapBean".equals(simpleName)) {
				Method method = clas.getDeclaredMethod("getListMap");
				List<Map<String, Object>> listMap = (List<Map<String, Object>>) method
						.invoke(this);
				return listMap;
			} else if ("SocketMapBean".equals(simpleName)) {
				Method method = clas.getDeclaredMethod("getMap");
				Map<String, Object> map = (Map<String, Object>) method
						.invoke(this);
				return map;
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
		return null;
	}

	public static SocketBean getSocketBean(Object msg) {
		SocketBean socketBean = null;
		String uuidStr = null;
		try {
			String resourceStr = msg.toString();
			String descurityStr = SecurityUtil.getSecurityStr(resourceStr,
					SecurityUtil.kek, 1);

			int descurityLen = descurityStr.indexOf("|");
			if (descurityLen == -1) {
				socketBean = new SocketBean();
				return socketBean;
			}

			String lengStr = descurityStr.substring(0, descurityLen);
			int length = Integer.parseInt(lengStr);

			String all_esc_str = descurityStr.substring(0, length);

			String all_read_Str = all_esc_str.substring(descurityLen + 1);

			int uuidIndex = all_read_Str.indexOf("|");

			uuidStr = all_read_Str.substring(0, uuidIndex);

			String next_four_Str = all_read_Str.substring(uuidIndex + 1);
			int serverIndex = next_four_Str.indexOf("|");
			String serverName = next_four_Str.substring(0, serverIndex);

			String next_two_Str = next_four_Str.substring(serverIndex + 1);
			int classNameIndex = next_two_Str.indexOf("|");
			String classNameStr = next_two_Str.substring(0, classNameIndex);

			String jsonStr = next_two_Str.substring(classNameIndex + 1);

			if ("SocketListMapBean".equals(classNameStr)) {
				List<Map<String, Object>> listMap = JsonUtil.toList(jsonStr);
				socketBean = new SocketListMapBean();
				Method method = socketBean.getClass().getDeclaredMethod(
						"setListMap", List.class);
				method.invoke(socketBean, listMap);
			} else if ("SocketMapBean".equals(classNameStr)) {
				Map<String, Object> map = JsonUtil.toHashMap(jsonStr);
				socketBean = new SocketMapBean();
				Method method = socketBean.getClass().getDeclaredMethod(
						"setMap", Map.class);
				method.invoke(socketBean, map);
			} else if ("SocketBean".equals(classNameStr)) {
				socketBean = new SocketBean();
			}
			QbbConfigBean configBean = QbbConfigUtil.getMethod(serverName);
			if (configBean == null) {
				String errorInfo = "server cant find servername[" + serverName
						+ "]";
				throw new Exception(errorInfo);
			}

			if (socketBean != null) {
				String modelName = configBean.getBeanName();
				String methodName = configBean.getMethodName();

				socketBean.setUuIdStr(uuidStr);
				socketBean.setModel(modelName);
				socketBean.setMethod(methodName);
			}
		} catch (Exception e) {
			HandlerException.error(e);
		} finally {
			if (socketBean == null) {
				socketBean = new SocketBean();
			}
			socketBean.setUuIdStr(uuidStr);
		}
		return socketBean;
	}

	public String getMethod() {
		return method;
	}

	public void setMethod(String method) {
		this.method = method;
	}

	public String getModel() {
		return model;
	}

	public void setModel(String model) {
		this.model = model;
	}

}
